package com.rocksea.app.weight;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Environment;
import android.util.Log;

import com.rocksea.app.db.SonicWave.SonicWave;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/1/17
 */

public class SonicWaveImage
{
    private static final int WAVE_HIGH = 21;

    public static File drawSonicWaveBitmap(List<SonicWave.SonicWaveData> sonicWaveDatas,String sectionId)
    {
        if (sonicWaveDatas.size()==0)
            return null;
        int w = 600;
        int h = WAVE_HIGH *(sonicWaveDatas.size()/2+1);
        Bitmap bitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_4444);

        Canvas canvas = new Canvas(bitmap);
        //初始化 画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Path path = new Path();

        //绘制背景色
        canvas.drawColor(Color.WHITE);
        int[] waveData = new int[sonicWaveDatas.get(0).waveData.length];
        for (int i = 0; i < sonicWaveDatas.size(); i++)
        {
            SonicWave.SonicWaveData sonicWaveData = sonicWaveDatas.get(i);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeWidth(4);
            paint.setTextSize(14);
            //绘制高度
            String depth = String.format("%.2fm",sonicWaveData.depth/1000f);
            Paint.FontMetrics fontMetrics =  paint.getFontMetrics();
            canvas.drawText(depth,5,(i+1)* WAVE_HIGH /2+Math.abs(fontMetrics.ascent),paint);
            //绘制曲线
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(2);
            for (int i1 = 0; i1 < sonicWaveData.waveData.length; i1++)
            {
                waveData[i1] = sonicWaveData.waveData[i1];
            }
            int maxWaveData = 0;
            for (Integer waveDatum : waveData)
            {
                if (maxWaveData < waveDatum)
                {
                    maxWaveData = waveDatum;
                }
            }
            //计算像素间隔
            float delta_x = (float) w/waveData.length;
            float delta_y = 1;
            if (maxWaveData!=0)
                delta_y = (float) WAVE_HIGH /(2*maxWaveData);
            path.reset();
            path.moveTo(0,(i+1)* WAVE_HIGH /2);
            canvas.drawPath(path,paint);
            for (int n = 0; n < sonicWaveData.waveData.length; n++)
            {
                path.lineTo(n*delta_x,(i+1)* WAVE_HIGH /2+sonicWaveData.waveData[n]*delta_y);
            }
            canvas.drawPath(path,paint);
            //绘制首波位置
            paint.setColor(Color.BLUE);
            paint.setStrokeWidth(2);
            canvas.drawLine(sonicWaveData.T1*delta_x,(i+1)* WAVE_HIGH /2-8,sonicWaveData.T1*delta_x,(i+1)* WAVE_HIGH /2+8,paint);
        }
        try
        {
            File filedir = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/SonicWaveImage");
            if (!filedir.exists())
            {
                if (filedir.mkdirs())
                {
                    Log.e("drawSonicWaveBitmap: ","创建文件夹成功" );
                }
                else
                {
                    Log.e("drawSonicWaveBitmap: ","创建文件夹失败" );
                }
            }
            File image = new File(filedir,String.format("%s.jpg",sectionId));
            FileOutputStream fileOutputStream = new FileOutputStream(image);
            Bitmap zipBitMap =  Bitmap.createScaledBitmap(bitmap,w,h,true);
            if (zipBitMap.compress(Bitmap.CompressFormat.JPEG,8,fileOutputStream))
            {
                fileOutputStream.flush();
                fileOutputStream.close();
                Log.d("drawSonicWaveBitmap: ","绘制完成");
            }
            else
            {
                Log.e("drawSonicWaveBitmap: ","开始写入本地失败");
            }
            System.out.println(image.getPath());
            return image;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
